<!--- 
LICENSE INFORMATION:

Copyright 2007, Hem Talreja
 
Licensed under the Apache License, Version 2.0 (the "License"); you may not 
use this file except in compliance with the License. 

You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License.

VERSION INFORMATION:	(0.001)

 --->
<cfcomponent extends="dbHelper">
	
	<cfset Variables._TableMeta = StructNew() />
	
	
	<cffunction name="init" access="public" returntype="dbMetaLoader" >
		<cfargument name="dbMetaRetreiver" type="any" required="true" />
		<cfargument name="dbProperties" type="any" required="true" />
		
		<!--- call setter functions --->
		<cfset setDBMetaRetreiver(Arguments.dbMetaRetreiver) />
		<cfset setDBProperties(Arguments.dbProperties) />
		
		<cfreturn this />
	</cffunction>
	
	
	<cffunction name="clearTableMeta" access="public" returntype="void" >
		<cfargument name="table" type="string" required="true" />
		
		<cfif StructKeyExists(Variables._TableMeta,Arguments.table)>
			<cfset StructDelete(Variables._TableMeta,Arguments.table) />
		</cfif>
		
	</cffunction>
	
	
	<cffunction name="getTableMeta" access="public" returntype="struct">
		<cfargument name="table" type="string" required="true" />
		
		<cfif NOT IsDefined("Variables._TableMeta.#Arguments.table#")>
			<cfset LoadTableMeta(table) />
		</cfif>
		
		<cfreturn Variables._TableMeta[table] />
	</cffunction>
	
	
	<cffunction name="getTableColumns" access="public" returntype="struct">
		<cfargument name="table" type="string" required="true" />
		
		<cfset var stcTableMeta = getTableMeta(Arguments.table) />
		
		<cfif IsDefined("stcTableMeta.Columns")>
			<cfreturn stcTableMeta.Columns /> 
		<cfelse>
			<cfthrow detail="columns not found for table [#Arguments.table#]" />	
		</cfif>
	</cffunction>
	
	
	<cffunction name="getTablePrimaryKey" access="public" returntype="string" >
		<cfargument name="table" type="string" required="true" />
		
		<cfset var stcTableMeta = getTableMeta(Arguments.table) />
		
		<cfif isDefined("stcTableMeta.Meta.PrimaryKey")>
			<cfreturn stcTableMeta.Meta.PrimaryKey />
		</cfif>
		
		<cfreturn "" />
	</cffunction>
	
	
	<cffunction name="getTableCounterKey" access="public" returntype="string" >
		<cfargument name="table" type="string" required="true" />
		
		<cfset var stcTableMeta = getTableMeta(Arguments.table) />
		
		<cfif isDefined("stcTableMeta.Meta.CounterKey")>
			<cfreturn stcTableMeta.Meta.CounterKey />
		</cfif>
	
		<cfreturn "0" />
	</cffunction>
	
	
	<cffunction name="getColumnDetail" access="public" returntype="string">
		<cfargument name="table" type="string" required="true" />
		<cfargument name="columnName" type="string" required="true" />
		<cfargument name="detailType" type="string" required="true" default="COLUMN_SIZE" />
		
		<cfset var stcColumnMeta = getTableColumns(Arguments.table) />
		
		<cfif isDefined("stcColumnMeta.#columnName#.#detailType#")>
			<cfreturn stcColumnMeta[columnName][detailType] />
		</cfif>
		
		<cfreturn "" />
	</cffunction>
	

	<cffunction name="getColumnSize" access="public" returntype="string" hint="returns the size of the column, to be used in future user interface enhancements.">
		<cfargument name="table" type="string" required="true" />
		<cfargument name="columnName" type="string" required="true" />
		
		<cfreturn getColumnDetail(table, columnName, "COLUMN_SIZE") />
	</cffunction>

	
	<cffunction name="LoadTableMeta" access="public" returntype="void">
		<cfargument name="table" type="string" required="true" />

		<cfset Variables._TableMeta[table] = getDBMetaRetreiver().getTableMeta(	getDBProperties().getMetaDataSource(), Arguments.Table, getDBProperties().getTableSchema() )  />
	</cffunction>
	
</cfcomponent>